package com.facebookdirectory.session;

import com.facebookdirectory.entity.*;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.framework.EntityQuery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.faces.event.ValueChangeEvent;


@Scope(ScopeType.PAGE)
@Name("fbAppInfoList")
public class FBAppInfoList extends EntityQuery<FBAppInfo> {

	private static final long serialVersionUID = 8240489250768768101L;

	private static final String ACTIVITY_EJBQL = "select fBAppInfo from FBAppInfo fBAppInfo where fBappInfo.category = 'Games' and fBappInfo.monthActive>10000 order by fBAppInfo.monthActive desc";
	private static final String NEWEST_EJBQL = "select fBAppInfo from FBAppInfo fBAppInfo where fBappInfo.category = 'Games' order by fBAppInfo.createTime desc";
	private static final String LATEST_UPDATE_EJBQL = "select fBAppInfo from FBAppInfo fBAppInfo where fBappInfo.category = 'Games' order by fBAppInfo.lastUpdate desc";
	private static final String EJBQL = "select fBAppInfo from FBAppInfo fBAppInfo where fBappInfo.category = 'Games' and fBappInfo.language=0";
	private static final String ZH_EJBQL = "select fBAppInfo from FBAppInfo fBAppInfo where fBappInfo.category = 'Games' and fBappInfo.language=1";

	private static final int PAGE_COUNT = 9;
	private FBAppInfo fBAppInfo = new FBAppInfo();

	private int type = 1;
	private Language language=Language.ENGLISH;
	
	public FBAppInfoList() {
		setSQL();		
		setMaxResults(15);
	}

	private void setSQL() {
		if(language==Language.CHINESE)
		{
			setEjbql(ZH_EJBQL);
		}
		else
		{
			setEjbql(EJBQL);
		}
		switch (type) {
		case 2:							
			this.setOrderColumn("lastUpdate");
			this.setOrderDirection("desc");
			break;
		case 1:	
			this.setOrderColumn("monthActive");
			this.setOrderDirection("desc");
			break;
		case 0:
			this.setOrderColumn("createTime");
			this.setOrderDirection("desc");
			break;
		}
	
	}

	public FBAppInfo getAppInfo() {
		return fBAppInfo;
	}

	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(int type) {
		this.getLog().info("type:" + type);
		this.type = type;
		setSQL();		
	}

	/**
	 * @return the page
	 */
	public int getPage() {
		Integer maxResult = getMaxResults();
		if (maxResult != null && maxResult != 0 && getFirstResult() != null) {
			return getFirstResult() / getMaxResults() + 1;
		}

		return 1;
	}

	public void selectPopular() {
		this.getLog().info("selectPopular");
	}

	/**
	 * Returns true if the previous page exists
	 */
	public boolean isPreviousPageExists() {
		return getPage() > 1;
	}

	/**
	 * Get the index of the first result of the previous page
	 * 
	 */
	public int getPreviousPage() {
		return (getPage() > 1) ? getPage() - 1 : 1;

	}

	public boolean isNextPageExists() {
		return getPage() < getPageCount();
	}

	/**
	 * Get the index of the first result of the next page
	 * 
	 */
	public int getNextPage() {
		return (getPage() < getPageCount()) ? getPage() + 1 : getPageCount();
	}

	public List<Integer> getPageList() {
		Integer i = 0;
		List<Integer> pages = new ArrayList<Integer>();
		int pageCount = getPageCount();
		int page = getPage();
		int end = page + PAGE_COUNT <= pageCount ? page + PAGE_COUNT
				: pageCount;
		int begin = end - 2 * PAGE_COUNT > 0 ? end - 2 * PAGE_COUNT : 1;

		for (i = begin; i <= end; i++) {
			pages.add(i);
		}
		return pages;
	}
	
	public void changeToChinese()
	{
		if(language!=Language.CHINESE)
		{
			language = Language.CHINESE;			
			setSQL();
			this.getLog().info("changeToChinese");
		}
	}
	
	public void changeToEnglish()
	{
		if(language!=Language.ENGLISH)
		{
			language = Language.ENGLISH;
			setSQL();
			this.getLog().info("changeToEnglish");
		}		
	}

}
